package com.example.village.controller;

import com.example.common.utils.R;
import com.example.village.entity.MessageEntity;
import com.example.village.entity.NeighbourhoodEntity;
import com.example.village.entity.PayEntity;
import com.example.village.entity.UserEntity;
import com.example.village.entityVO.*;
import com.example.village.mqtt.TopicConsumerListener;
import com.example.village.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.*;

import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;
import java.util.Arrays;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.*;

@Api(tags = "管理员用户接口")
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private MessageService messageService;

    @Autowired
    private NeighbourhoodService neighbourhoodService;

    @Autowired
    private PayService payService;

    @Autowired
    private SubscribeMsgService subscribeMsgService;

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @ApiOperation("模糊查找用户")
    @RequestMapping(value = "/findUser", method = POST)
    public R findUser(@RequestBody UserDimVO vo) {
        List<UserEntity> users = userService.queryByDim(vo);
        return R.ok().put("list", users);
    }

    @ApiOperation("查找所有用户")
    @RequestMapping(value = "/querySelect", method = GET)
    public R querySelect() {
        List<UserVO> users = userService.querySelect();
        return R.ok().put("list", users);
    }

    @ApiOperation("根据id查找所有用户")
    @RequestMapping(value = "/queryById", method = GET)
    public R queryById(Integer id) {
        UserEntity users = userService.queryById(id);
        return R.ok().put("list", users);
    }

    @ApiOperation("添加用户")
    @RequestMapping(value = "/addUser", method = POST)
    public R addUser(@RequestBody UserEntity user) {
        if (user.getState() == null) {
            user.setState(1);
        }
        if (userService.add(user)) {
            return R.ok("添加成功！");
        }
        return R.error("添加失败！");
    }

    @ApiOperation("修改用户")
    @RequestMapping(value = "/updateUser", method = POST)
    public R updateUser(@RequestBody UserEntity user) {
        if (userService.update(user)) {
            return R.ok("修改成功！");
        }
        return R.error("修改失败！");
    }

    @ApiOperation("删除用户")
    @RequestMapping(value = "/deleteUser", method = PUT)
    public R deleteUser(@RequestParam Integer[] ids) {
        List<Integer> Ids = Arrays.asList(ids);
        if (userService.deleteMore(Ids)) {
            return R.ok("删除成功！");
        }
        return R.error("删除失败！");
    }

    @ApiOperation("模糊查找小区")
    @RequestMapping(value = "/findNeigh", method = GET)
    public R findNeigh(String word) {
        List<NeighbourhoodEntity> users = neighbourhoodService.queryByDim(word);
        return R.ok().put("list", users);
    }

    @ApiOperation("查找所有小区")
    @RequestMapping(value = "/queryNSelect", method = GET)
    public R queryNSelect() {
        List<UserVO> users = neighbourhoodService.querySelect();
        return R.ok().put("list", users);
    }

    @ApiOperation("模糊查找所有小区")
    @RequestMapping(value = "/queryNSelectByParam", method = GET)
    public R queryNSelectByParam(String param) {
        List<UserVO> users = neighbourhoodService.querySelectByParam(param);
        return R.ok().put("list", users);
    }

    @ApiOperation("根据id查找所有用户")
    @RequestMapping(value = "/queryNById", method = GET)
    public R queryNById(Integer id) {
        NeighbourhoodEntity users = neighbourhoodService.queryById(id);
        return R.ok().put("list", users);
    }

    @ApiOperation("添加小区")
    @RequestMapping(value = "/addNeigh", method = POST)
    public R addNeigh(@RequestBody NeighbourhoodEntity entity) {
        if (neighbourhoodService.insert(entity)) {
            return R.ok("添加成功！");
        }
        return R.error("添加失败！");
    }

    @ApiOperation("修改小区")
    @RequestMapping(value = "/updateNeigh", method = POST)
    public R updateNeigh(@RequestBody NeighbourhoodEntity entity) {
        if (neighbourhoodService.update(entity)) {
            return R.ok("修改成功！");
        }
        return R.error("修改失败！");
    }

    @ApiOperation("删除小区")
    @RequestMapping(value = "/deleteNeigh", method = PUT)
    public R deleteNeigh(@RequestParam Integer[] ids) {
        List<Integer> Ids = Arrays.asList(ids);
        if (neighbourhoodService.deleteMore(Ids)) {
            return R.ok("删除成功！");
        }
        return R.error("删除失败！");
    }

    @ApiOperation("模糊查找缴费")
    @RequestMapping(value = "/findPay", method = POST)
    public R findPay(@RequestBody PayDimVO vo) {
        List<PayVO> users = payService.queryByDim(vo);
        return R.ok().put("list", users);
    }

    @ApiOperation("添加缴费")
    @RequestMapping(value = "/addPay", method = POST)
    public R addPay(@RequestBody PayEntity entity) {
        if (payService.insert(entity)) {
            return R.ok("添加成功！");
        }
        return R.error("添加失败！");
    }

    @ApiOperation("修改缴费")
    @RequestMapping(value = "/updatePay", method = POST)
    public R updatePay(@RequestBody PayEntity entity) {
        if (payService.update(entity)) {
            return R.ok("添加成功！");
        }
        return R.error("添加失败！");
    }

    @ApiOperation("删除缴费")
    @RequestMapping(value = "/deletePay", method = PUT)
    public R deletePay(@RequestParam Integer[] ids) {
        List<Integer> Ids = Arrays.asList(ids);
        if (payService.deleteMore(Ids)) {
            return R.ok("删除成功！");
        }
        return R.error("删除失败！");
    }

    @ApiOperation("模糊查找信息")
    @RequestMapping(value = "/findMsg", method = POST)
    public R findMsg(MessageDTO dto) {
        List<MessageVO> users = messageService.queryVOByDim(dto);
        return R.ok().put("list", users);
    }

    @ApiOperation("添加信息")
    @RequestMapping(value = "/addMessage", method = POST)
    public R addMessage(@RequestBody MessageEntity entity) {
        if (messageService.insert(entity)) {
            this.sendMessage(new ActiveMQTopic("msg"), "{\"" + entity.getnId() + "\":\"" + entity.getMessage() + "\"}");
            return R.ok("添加成功！");
        }
        return R.error("添加失败！");
    }

    @ApiOperation("修改信息")
    @RequestMapping(value = "/updateMessage", method = POST)
    public R updateMessage(@RequestBody MessageEntity entity) {
        if (messageService.update(entity)) {
            this.sendMessage(new ActiveMQTopic("msg"), "{\"" + entity.getnId() + "\":\"" + entity.getMessage() + "\"}");
            return R.ok("修改成功！");
        }
        return R.error("修改失败！");
    }

    @ApiOperation("删除信息")
    @RequestMapping(value = "/deleteMessage", method = PUT)
    public R deleteMessage(@RequestParam Integer[] ids) {
        List<Integer> Ids = Arrays.asList(ids);
        if (messageService.deleteMore(Ids)) {
            return R.ok("删除成功！");
        }
        return R.error("删除失败！");
    }

    @ApiOperation("发送门禁消息")
    @RequestMapping(value = "/topic/door", method = POST)
    public String sendDoor(@RequestBody String str) {
        this.sendMessage(new ActiveMQTopic("door"), str);
        return "success";
    }

    @ApiOperation("发送灯光消息")
    @RequestMapping(value = "/topic/lighting", method = POST)
    public String sendLighting(@RequestBody String str) {
        this.sendMessage(new ActiveMQTopic("lighting"), str);
        return "success";
    }

    @ApiOperation("灯光返回接收")
    @RequestMapping(value = "/isSuccess", method = POST)
    public String isSuccess() {
        return TopicConsumerListener.is_success;
    }

    @ApiOperation("大门返回接收")
    @RequestMapping(value = "/isDoorSuccess", method = POST)
    public String isDoorSuccess() {
        return TopicConsumerListener.door_success;
    }

    // 发送消息，destination是发送到的队列，message是待发送的消息
    private void sendMessage(Destination destination, final String message){
        jmsMessagingTemplate.convertAndSend(destination, message);
    }
}
